using ObjFileLoader.Common;
using ObjFileLoader.Data.Elements;
using ObjFileLoader.Data.VertexData;
using System.Collections.Generic;
using System.Linq;

namespace ObjFileLoader.Data.DataStore
{
	public class DataStore : IDataStore, IGroupDataStore, IVertexDataStore, ITextureDataStore, INormalDataStore, IFaceGroup, IMaterialLibrary, IElementGroup
	{
		private Group _currentGroup;

		private readonly List<Group> _groups = new List<Group>();

		private readonly List<Material> _materials = new List<Material>();

		private readonly List<Vertex> _vertices = new List<Vertex>();

		private readonly List<Texture> _textures = new List<Texture>();

		private readonly List<Normal> _normals = new List<Normal>();

		public IList<Vertex> Vertices => _vertices;

		public IList<Texture> Textures => _textures;

		public IList<Normal> Normals => _normals;

		public IList<Material> Materials => _materials;

		public IList<Group> Groups => _groups;

		public void AddFace(Face face)
		{
			PushGroupIfNeeded();
			_currentGroup.AddFace(face);
		}

		public void PushGroup(string groupName)
		{
			_currentGroup = new Group(groupName);
			_groups.Add(_currentGroup);
		}

		private void PushGroupIfNeeded()
		{
			if (_currentGroup == null)
			{
				PushGroup("default");
			}
		}

		public void AddVertex(Vertex vertex)
		{
			_vertices.Add(vertex);
		}

		public void AddTexture(Texture texture)
		{
			_textures.Add(texture);
		}

		public void AddNormal(Normal normal)
		{
			_normals.Add(normal);
		}

		public void Push(Material material)
		{
			_materials.Add(material);
		}

		public void SetMaterial(string materialName)
		{
			Material material = _materials.SingleOrDefault((Material x) => x.Name.EqualsOrdinalIgnoreCase(materialName));
			PushGroupIfNeeded();
			_currentGroup.Material = material;
		}
	}
}
